﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess_Layer
{
    public static class Payment_DAC
    {
        public static int payoutUsingSS(long cusID, int stateID, long centerID, string productDetail, string bonusDetail, out int productIdCauseError, out int bonusIdCauseError) {
            bonusIdCauseError = -1;
            productIdCauseError = -1;
            List<SqlParameter> sqlParams = makePayoutParams(ref cusID, ref stateID, ref centerID, ref productDetail, ref bonusDetail, ref productIdCauseError, ref bonusIdCauseError);

            DataAccessHelper helper = null;
            SqlDataReader rdr = null;
            int result;
            try
            {
                helper = new DataAccessHelper();
                rdr = helper.excuteSP("SP_Payout", sqlParams);
            }
            finally
            {
                helper.close();
                rdr.Close();

                // If your Command contains output parameters or return values, they will not be available until the DataReader is closed.            
                result = (int)sqlParams[sqlParams.Count - 1].Value;
            }
            
            productIdCauseError = int.Parse(sqlParams[sqlParams.Count - 2].Value.ToString());
            bonusIdCauseError = int.Parse(sqlParams[sqlParams.Count - 1].Value.ToString());
            
            return result;
        }
        private static List<SqlParameter> makePayoutParams(ref long cusID, ref int stateID, ref long centerID, ref string productDetail, ref string bonusDetail, ref int productIdCauseError, ref int bonusIdCauseError, bool usingSS = true)
        {
            List<SqlParameter> sqlParams = new List<SqlParameter>();
            
            SqlParameter inParam = new SqlParameter("@customerID", SqlDbType.BigInt);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = cusID;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@centerID", SqlDbType.BigInt);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = centerID;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@stateID", SqlDbType.BigInt);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = stateID;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@productDetailString", SqlDbType.VarChar, 1000);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = productDetail;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@bonusDetailString", SqlDbType.VarChar, 1000);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = bonusDetail;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@ErrorProductID", SqlDbType.Int);
            inParam.Direction = ParameterDirection.Output;
            inParam.Value = productIdCauseError;
            sqlParams.Add(inParam);

            inParam = new SqlParameter("@ErrorBonusID", SqlDbType.Int);
            inParam.Direction = ParameterDirection.Output;
            inParam.Value = bonusIdCauseError;
            sqlParams.Add(inParam);

            if (!usingSS)
            {
                inParam = new SqlParameter("@useSS", SqlDbType.Int);
                inParam.Direction = ParameterDirection.Input;
                inParam.Value = 0;
                sqlParams.Add(inParam);
            }

            return sqlParams;
        }

        public static int payoutUsingBank(long cusID, int stateID, long centerID, string productDetail, string bonusDetail, out int productIdCauseError, out int bonusIdCauseError)
        {
            bonusIdCauseError = -1;
            productIdCauseError = -1;
            List<SqlParameter> sqlParams = makePayoutParams(ref cusID, ref stateID, ref centerID, ref productDetail, ref bonusDetail, ref productIdCauseError, ref bonusIdCauseError, false);

            DataAccessHelper helper = null;
            SqlDataReader rdr = null;
            int result;
            try
            {
                helper = new DataAccessHelper();
                rdr = helper.excuteSP("SP_Payout", sqlParams);
            }
            finally
            {
                helper.close();
                rdr.Close();

                // If your Command contains output parameters or return values, they will not be available until the DataReader is closed.            
                result = (int)sqlParams[sqlParams.Count - 1].Value;
            }

            productIdCauseError = int.Parse(sqlParams[sqlParams.Count - 2].Value.ToString());
            bonusIdCauseError = int.Parse(sqlParams[sqlParams.Count - 1].Value.ToString());

            return result;
        }
    }
}
